import { queryLogOut } from '@/api/userApi'
import dayjs from 'dayjs'
import { currentHost } from './baseVariable'
const OBJECT_NAME = '视频会议管理系统'
// interface DateTypeProps extent DateType=string
export declare type DateTypeProps = string | String
// 所有的时间都按2021-07-29T16:30:00+08:00[后台统一给的时间格式/接口发送参数也要求该格式]
/**把时间转成 2021-07-29T16:30:00+08:00*/
const formatTDate = (dateStr: DateTypeProps) => {
	var str = dateStr.split(' ').join('T') + '+08:00' //"2021-07-29T16:30:00+08:00"
	return str
}
/**把时间转成 2021-07-29T16:30:00+08:00 并 转义一下*/
const encodeURIFormatTDate = (dateStr: DateTypeProps) => {
	var str = dateStr.split(' ').join('T') + '+08:00' //"2021-07-29T16:30:00+08:00"
	return encodeURIComponent(str)
}
// '2021-07-29'
/**把时间转成 2021-07-29*/
const formatDate = (dateStr: string) => {
	var str = dayjs(dateStr).format('YYYY-MM-DD')
	return str
}
/**把时间转成 2021-07-29 00:00:00*/
const formatDateZero = (dateStr: string) => {
	var str = dayjs(dateStr).format('YYYY-MM-DD') + ' 00:00:00'
	return str
}
/**把时间转成 2021-07-29 23:59:59*/
const formatDateMax = (dateStr: string) => {
	var str = dayjs(dateStr).format('YYYY-MM-DD') + ' 23:59:59'
	return str
}
/**把时间转成 2021-07-29 16:30:00*/
const formatDateTime = (dateStr: string) => {
	var str = dayjs(dateStr).format('YYYY-MM-DD HH:mm:ss')
	return str
}
/**判断单个值是否为空 */
const isBlank = (value: any): boolean => {
	if (value === null || value === undefined || value === '') {
		return true
	}
	return false
}
/**判断object中所有对象内容是否为空 */
const isAllEmptyValue = (value: { [x: string]: any }): boolean => {
	// 若对象为空则返回true
	if (isBlank(value)) {
		return true
	}
	if (typeof value === 'object') {
		if (Object.keys(value).every((item) => isBlank(value[item]))) {
			return true
		} else {
			return false
		}
	} else {
		return true
	}
}
let randomStr = Math.random().toString(36).slice(-6) //生成一个随机数
const encode = (key: string | number) => {
	return encodeURIComponent(key)
}
/**
 * 获取地址栏参数
 * @param keyName 参数名
 */
const getQueryString = (keyName: string) => {
	var reg = new RegExp('(^|&)' + keyName + '=([^&]*)(&|$)', 'i')
	var r = window.location.search.substr(1).match(reg)
	if (r) {
		return unescape(r[2])
	} else {
		return ''
	}
}

/**
 *是否是机密增强
 * @param level
 * @returns
 */
export const isSecretEnhance = (level: number) => {
	if (level === 3) {
		return true
	}
	return false
}
/**
 *获取平台密级
 * @param level
 */
export const getSystemLevel = (level: number) => {
	if (isSecretEnhance(level) || level === 2) {
		//机密
		return 'confidential'
	} else if (level === 1) {
		//秘密
		return 'secret'
	} else {
		//公开
		return 'open'
	}
}
/**
 * 获取整数占比
 * @param used 已使用数
 * @param total 总数
 */
const getIntPercent = (used: number, total: number) => {
	let percent = total === 0 ? 0 : used / total
	percent = Math.round(percent * 100)
	if (percent === 0 && used > 0) {
		return 1
	}
	if (percent === 100 && used < total) {
		return 99
	}
	return percent
}
/**登出*/
const quitLogin = () => {
	queryLogOut({}).then(() => {
		localStorage.removeItem('system')
		localStorage.removeItem('user')
		window.location.href = currentHost + '/portal/login'
	})
}
/**
 *
 * @param {*} timeInterval  等待时间间隔 默认用户十分钟没有操作，十分钟后退出登录页面
 * @param {*} callBack  回调函数 默认跳转到登录界面
 */
function recordUserOperationBehavior(
	timeInterval: number = 1000 * 60 * 10,
	callBack: Function = function () {
		window.location.href = '/portal/login' //跳转到登录界面
	}
) {
	window.userBehaviorStartTime = new Date().getTime() // 进入会管时间
	// 检测操作函数
	var behaviorDisengagementOperation = function (time: number, cb: Function) {
		if (window.userBehaviorInterval) return
		window.userBehaviorInterval = setTimeout(function () {
			if (time <= new Date().getTime() - window.userBehaviorStartTime) {
				cb()
			}
		}, time)
	}
	// 执行检测函数
	behaviorDisengagementOperation(timeInterval, callBack)
	document.addEventListener('mousemove', function () {
		window.userBehaviorStartTime = new Date().getTime() // 刷新操作时间
		clearTimeout(window.userBehaviorInterval) // 清除延时器钩子
		window.userBehaviorInterval = undefined
		behaviorDisengagementOperation(timeInterval, callBack) // 重新计时添加计时时间
	})
	document.addEventListener('keydown', function () {
		window.userBehaviorStartTime = new Date().getTime()
		clearTimeout(window.userBehaviorInterval)
		window.userBehaviorInterval = undefined
		behaviorDisengagementOperation(timeInterval, callBack)
	})
	window.addEventListener('message', function (event) {
		if (event.data === 'behavior:mousemove' || event.data === 'behavior:keydown') {
			window.userBehaviorStartTime = new Date().getTime()
			clearTimeout(window.userBehaviorInterval)
			window.userBehaviorInterval = undefined
			behaviorDisengagementOperation(timeInterval, callBack)
		}
	})
}
export {
	formatTDate, //2021-07-29T16:30:00+08:00
	formatDate, //'2021-07-29'
	formatDateTime, //'2021-07-29 16:30:00'
	formatDateZero,
	formatDateMax,
	encodeURIFormatTDate,
	randomStr,
	encode,
	getQueryString,
	getIntPercent,
	quitLogin,
	isAllEmptyValue,
	isBlank,
	recordUserOperationBehavior,
	OBJECT_NAME,
}
